package com.qiall.boot.util.controller;

import com.alibaba.fastjson.JSON;
import com.qiall.boot.util.controller.util.BuildSqlUtil;
import com.qiall.boot.util.entity.TreeEntity;
import com.qiall.boot.util.request.GetTreeRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TreeBuildController {

    @PostMapping("/getTreeEntity")
    public TreeEntity getTreeEntity(@RequestBody GetTreeRequest getTreeRequest) throws ClassNotFoundException, SQLException {
        // 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2.获取连接
        String url = getTreeRequest.getUrl();
        String user = getTreeRequest.getUserName();
        String password = getTreeRequest.getPassword();
        Connection conn = DriverManager.getConnection(url, user, password);
        // 查询根节点
        Map<String, Object> rootNode = getRootNode(getTreeRequest, conn.createStatement());
        TreeEntity treeEntity = new TreeEntity();
        treeEntity.setId(getTreeRequest.getRootId());
        treeEntity.setData(JSON.toJSONString(rootNode));
       Object rootNodeNameObj= rootNode.get(getTreeRequest.getNodeNameColName());
       if(rootNodeNameObj!=null){
           treeEntity.setName(rootNodeNameObj.toString());
       }

        Statement statement = conn.createStatement();
        buildTree(treeEntity, getTreeRequest, statement);
        statement.close();
        conn.close();
        return treeEntity;

    }

    private Map<String, Object> getRootNode(GetTreeRequest request, Statement statement) throws SQLException {

        String sql = "select * from table_name where cole_name=col_value";
        if(StringUtils.isBlank(request.getRootId())){
            Map<String, Object>    retMap=new HashMap<>();
            retMap.put(request.getNodeColName(), "空节点");
            retMap.put(request.getNodeColName(), null);
            return retMap;
        }
        Map<String, String> map = new HashMap<>();
        map.put("table_name", request.getTableName());
        map.put("cole_name", request.getNodeColName());
        map.put("col_value", request.getRootId());
        String sqlForQuery = BuildSqlUtil.buildSql(map, sql);
        Map<String, Object> retMap = BuildSqlUtil.queryForOne(sqlForQuery, statement);
        if (MapUtils.isEmpty(retMap)) {
            retMap.put(request.getNodeColName(), "空节点");
            retMap.put(request.getNodeColName(), null);
        }
        statement.close();
        return retMap;
    }

    private List<Map<String, Object>> getChildren(GetTreeRequest request, Object parentId, Statement statement) throws SQLException {
        String sql = "select * from table_name where parent_col=parent_value";
        Map<String, String> map = new HashMap<>();
        map.put("table_name", request.getTableName());
        map.put("parent_col", request.getParentNodeColName());
        if (parentId!=null&&StringUtils.isNotBlank(parentId.toString())) {
            map.put("parent_value", parentId.toString());
        } else {
            sql = "select * from table_name where parent_col is null";
        }
        String sqlForQuery = BuildSqlUtil.buildSql(map, sql);
        return BuildSqlUtil.queryForList(sqlForQuery, statement);
    }

    private void buildTree(TreeEntity treeEntity, GetTreeRequest request, Statement statement) throws SQLException {

        List<Map<String, Object>> children = getChildren(request, treeEntity.getId(), statement);
        if (CollectionUtils.isEmpty(children)) {
            return;
        }
        treeEntity.setChildren(new ArrayList<>());
        for (Map<String, Object> stringObjectMap : children) {
            if (MapUtils.isEmpty(stringObjectMap)) {
                continue;
            }
            TreeEntity child = new TreeEntity();
            child.setParentId(treeEntity.getId());

            Object object = stringObjectMap.get(request.getNodeColName());
            if (object != null) {
                child.setId(object.toString());
            }
            Object nodeColName = stringObjectMap.get(request.getNodeNameColName());
            if (nodeColName != null) {
                child.setName(nodeColName.toString());
            }
            child.setData(JSON.toJSONString(stringObjectMap));
            buildTree(child, request, statement);
            treeEntity.getChildren().add(child);
        }
    }

}
